<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/23 0023
 * Time: 10:16
 */
require_once './Request.php';
require_once './Log.php';
class Task
{
    private static $pre = 'php_timeout_task';
    public static function add($url,$date,$taskName='')
    {
        ini_set('date.timezone','PRC');
        $data = json_encode([
            'url'=>$url,
            'task_name'=>$taskName
        ]);
        $key = self::$pre.$data;
        $ex = strtotime($date)-time();

        if($ex <= 0){
            throw new Exception('date config error');
            exit();
        }
        echo $ex;
        return RedisOp::instance()->setex($key,$ex,'1');
    }

    public static function exec($key)
    {
        $json = str_replace(self::$pre,'',$key);
        $data = json_decode($json,true);
        $res = Request::get($data['url']);
        Log::instance()->info($res);
    }
}